//
//  GuidePlayerViewController.m
//  Myrna-Mindfulness
//
//  Created by Anders J.R. Petersen on 03/09/13.
//  Copyright (c) 2013 Anders J.R. Petersen. All rights reserved.
//

#import "GuidePlayerViewController.h"

@interface GuidePlayerViewController (){
    AVAudioPlayer *avPlayer;
}

@property NSString *headerStr;
@property NSString *detailsStr;

@end

@implementation GuidePlayerViewController
    

@synthesize header;
@synthesize headerStr;

@synthesize details;
@synthesize detailsStr;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)setHeaderText:(NSString *)str{
    
    headerStr = str;
    
}

-(void)setDetailsText:(NSString *)str{
    
    detailsStr = str;
    
}

- (IBAction)backToStartView:(id)sender {
    [avPlayer stop];
}

- (IBAction)playButtonAction:(id)sender {
    [avPlayer play];
}

- (IBAction)pauseButtonAction:(id)sender {
    [avPlayer pause];
}

- (IBAction)restartButtonAction:(id)sender {
    [avPlayer stop];
    [avPlayer setCurrentTime:0];
}

- (IBAction)backToOverViewAndStop:(id)sender {
    [avPlayer stop];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    [avPlayer stop];
}

-(void)initializePlayer:(NSString *) soundFile{
    NSString *stringPath =[[NSBundle mainBundle]pathForResource:soundFile ofType:@"mp3"];
    NSURL *url;
    
    if(stringPath){
        url = [NSURL fileURLWithPath:stringPath];
    }else{
        //maybe we have a complete url of a downloaded file
        url = [NSURL fileURLWithPath:soundFile];
    }
    
    NSError *error;
    
    avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    
    [avPlayer setNumberOfLoops:-1];
    
    [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(updateProgressView) userInfo:nil repeats:YES];
    
    [avPlayer play];
    
}

-(void)updateProgressView{
    float progress = [avPlayer currentTime]/[avPlayer duration];
    self.progressBar.progress = progress;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    header.text = headerStr;
    details.text = detailsStr;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
